<?php
/*
 *	Kernel class
 *
 *	@abstract
 */
abstract class Kernel
{
	/*
	 *	$instances array Instance variable
	 *
	 *	@protected
	 *	@static
	 */
	protected static $instances = array();
	
	/*
	 *	Constructor
	 *
	 *	@protected
	 */
	protected function __construct(){}
	
	/*
	 *	Disable objects cloning
	 *
	 *	@protected
	 *	@final
	 */
	final protected function __clone(){}
	
	/*
	 *	Return instance of object
	 *	
	 *	@param	string	Object indetificator [optional]
	 *	@return	object	Created or exists object
	 *	@throw
	 *	@static
	 */
	static public function getInstance($id = null)
	{
		$class = get_called_class();
		
		$pointer = $class.(isset($id) ? '.'.$id : '');
		
		if(!array_key_exists($pointer, self::$instances))
		{
			if(!class_exists($class))
			{
				throw new Exception('Class "'.$class.'" does not exists');
			}
			
			self::$instances[$pointer] = new $class();
		}
		
		return self::$instances[$pointer];
	}
	
	/*
	 *	Destroy instance of object
	 *
	 *	@param	string	Object indetificator [optional]
	 *	@return	boolean
	 *	@static
	 */
	static public function destroyInstance($id = null)
	{
		$class = get_called_class();
		
		$pointer = $class.(isset($id) ? '.'.$id : '');
		
		if(array_key_exists($pointer, self::$instances))
		{
			if(method_exists($class, '__destruct'))
			{
				$class::getInstance()->__destruct();
			}
			
			unset(self::$instances[$pointer]);
		}
	}
}